package com.ruoyi.system.service.impl;

import java.io.File;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.Formalities;
import com.ruoyi.system.mapper.FormalitiesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.QualificationMapper;
import com.ruoyi.system.domain.Qualification;
import com.ruoyi.system.service.IQualificationService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2021-04-29
 */
@Service
public class QualificationServiceImpl implements IQualificationService {
    @Autowired
    private QualificationMapper qualificationMapper;
    @Autowired
    private FormalitiesMapper formalitiesMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public Qualification selectQualificationById(Long id) {
        try {

            Qualification qualification = qualificationMapper.selectQualificationById(id);
            Formalities formalities = new Formalities();
            formalities.setQualiId(id);
            qualification.setFormalities(formalitiesMapper.selectFormalitiesList(formalities));
            return qualification;
        } catch (Exception exception) {
            return new Qualification();
        }
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param qualification 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Qualification> selectQualificationList(Qualification qualification) {
        List<Qualification> qualifications = qualificationMapper.selectQualificationList(qualification);
        //资质图片
        for (int i = 0; i < qualifications.size(); i++) {
            Formalities formalities = new Formalities();
            formalities.setQualiId(qualifications.get(i).getId());
            qualifications.get(i).setFormalities(formalitiesMapper.selectFormalitiesList(formalities));
        }
        return qualifications;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param qualification 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertQualification(Qualification qualification) {
        return qualificationMapper.insertQualification(qualification);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param qualification 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateQualification(Qualification qualification) {
        return qualificationMapper.updateQualification(qualification);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteQualificationByIds(Long[] ids) {
        int a=0;
        String profile = RuoYiConfig.getProfile();
        try {
            for (int i = 0; i <ids.length ; i++) {
                Formalities formalities = formalitiesMapper.selectFormalitiesById(ids[i]);
                formalities.setUrl(formalities.getUrl().replace("/profile", profile));
                formalities.setUrl(formalities.getUrl().replace("profile", profile));
                if (new File(formalities.getUrl()).delete()) {
                    a+=1;
                }
            }
            formalitiesMapper.deleteFormalitiesByqualiIds(ids);
            qualificationMapper.deleteQualificationByIds(ids);
        }catch (NullPointerException e){

        }
        return a;
    }

    @Override
    public int deleteFormalitiesByqualiId(Long id) {
        return formalitiesMapper.deleteFormalitiesByqualiId(id);
    }

    /**
     * 删除【请填写功能名称】信息
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteQualificationById(Long id) {
        qualificationMapper.deleteQualificationById(id);
        return qualificationMapper.deleteFormalitiesByqualiId(id);
    }


}
